
$(function() {
	Tip.emp('员工');	
});

var Exten = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.cpu = form.cpu.value;
				body.mb = form.mb.value;
				body.memory = form.memory.value;
				body.display = form.display.value;
				body.disk = form.disk.value;

				body.cdrom = form.cdrom.value;
				body.monitor = form.monitor.value;
				body.box = form.box.value;
				body.kb = form.kb.value;
				body.mouse = form.mouse.value;

				body.purpose = form.purpose.value;
				body.content = form.content.value;
				body.buydate = form.buydate.value;

				$(this).dialog("close");

				$.post('info.php?do=Computer.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	popup: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var a1 = ['编辑信息', 'url', 'Exten.dialog_edit_', '650x420'];
		var a2 = ['复制信息', 'url', 'Exten.dialog_copy_', '500x300'];
		var a3 = ['拔打权限', 'url', 'Exten.dialog_co_', '550x320'];

		var c1 = 'emp.php?do=Exten.edit&id=' + id + p2;
		var c2 = 'emp.php?do=Exten.copy&id=' + id + p2;
		var c3 = 'emp.php?do=Exten.co&id=' + id + p2;

		var s = c1 + ", " + c2 + ", " + c3;
		popup(e, [a1, a2, a3], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				
				var cf = '';
				var cfu = '';
				var cfb = '';
				var dnd = 'NO';
				
				var regex = /^(1906|1966006|2|6|8)(\d{3,5})$/
				
				if ($("#c_unconditional").attr("checked")) {
					cf = form.exten_unconditional.value;
					/*
					if (!regex.test(cf)) {
						alert("分机设置有误，请重新设置！");
						return true;
					}*/
				}
				
				if ($("#c_unavailable").attr("checked")) {
					cfu = form.exten_unavailable.value;
					/*
					if (!regex.test(cfu)) {
						alert("分机设置有误，请重新设置！");
						return true;
					}*/
				}
				
				if ($("#c_busy").attr("checked")) {
					cfb = form.exten_busy.value;
					/*
					if (!regex.test(cfb)) {
						alert("分机设置有误，请重新设置！");
						return true;
					}*/
				}
				
				if ($("#c_dnd").attr("checked")) {
					dnd = 'YES';
				}
				
				body.id = form.id.value;
				body.emp = form.emp.value;
				
				body.name = form.name.value;
				body.exten = form.exten.value;
				body.timeout = form.timeout.value;
				
				body.record_in = form.record_in.value;
				body.record_out = form.record_out.value;
				
				body.cf = cf;
				body.cfu = cfu;
				body.cfb = cfb;
				body.dnd = dnd;

				$(this).dialog("close");

				var url = 'emp.php?do=Exten.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;
				body.exten = form.exten.value;

				body.record_in = form.record_in.value;
				body.record_out = form.record_out.value;

				$(this).dialog("close");

				var url = 'emp.php?do=Exten.copyPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_co_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.emp = form.emp.value;
				body.exten = form.exten.value;
				body.cf = form.cf.value;
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;
				
				var co = new Array();
				$("input[name^='co_']").each(function() {
					if ($(this).attr("checked")) {
						co.push($(this).val());
					}
				})
				
				body.co = $.toJSON(co);

				$(this).dialog("close");

				var url = 'emp.php?do=Exten.coPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	}	
};
